package com.example.exam.controller;

import com.example.exam.pojo.ManagementUser;
import com.example.exam.service.ManagementUserService;
import com.example.exam.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.List;

/**
 * @author
 * @date 2023/03/09
 * 管理员管理
 */
@RequestMapping("/management")
@RestController
@RequiredArgsConstructor
public class ManagementController  {

    private final ManagementUserService managementUserService;

    /**
     *  管理员登录
     */
    @PostMapping("/login")
    public Object login(ManagementUser managementUser) {
        ManagementUser user = managementUserService.searchAllByUsername(managementUser.getUsername());
        if(user == null) {
            return R.fail("用户不存在");
        }else if(!user.getPassword().equals(managementUser.getPassword())){
            return R.fail("密码错误");
        }else {
            String str = user.getUsername() + ":" + user.getPermission();
            String token = Base64.getEncoder().encodeToString(str.getBytes());
            return R.ok(token);
        }
    }

    /**
     * 管理员列表
     */
    @GetMapping("/getAdministratorList")
    public Object getAdministratorList() {
        List<ManagementUser> managementUsers = managementUserService.searchAll();
        return R.ok(managementUsers);
    }

    /**
     * 添加管理员
     */
    @PostMapping("/insertAdministrator")
    public Object insertAdministrator(ManagementUser managementUser, HttpServletRequest request) {
        System.out.println(managementUser);
        int i = decode(request);
        if(i < 1) {
            return R.fail("权限不够");
        }
        ManagementUser user = managementUserService.searchAllByUsername(managementUser.getUsername());
        if(user != null) {
            return R.fail("该用户已存在");
        }
        managementUserService.insert(managementUser);
        return R.ok();
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Object updatePassword(ManagementUser managementUser, HttpServletRequest request) {
        int i = decode(request);
        if(i < 1) {
            return R.fail("权限不够");
        }
        ManagementUser user = managementUserService.searchAllByUsername(managementUser.getUsername());
        if(user == null) {
            return R.fail("该用户不存在");
        }
        managementUserService.update(managementUser);
        return R.ok();
    }

    /**
     * 删除管理员
     */
    @DeleteMapping("/delAdministrator")
    public Object delAdministrator(String username, HttpServletRequest request) {
        int i = decode(request);
        if(i < 1) {
            return R.fail("权限不够");
        }
        ManagementUser user = managementUserService.searchAllByUsername(username);
        if(user == null) {
            return R.fail("该用户不存在");
        }
        if ("root".equals(user.getUsername())) {
            return R.fail("超级管理员不能删除");
        }
        managementUserService.del(username);
        return R.ok();
    }

    /**
     * 解析token
     */
    private int decode(HttpServletRequest request) {
        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        String[] strings = new String(Base64.getDecoder().decode(token.getBytes())).split(":");
        return Integer.parseInt(strings[1]);
    }
}
